home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
MACSHELL
/
MS1
/
SHELL_SO
/
STRPATTE.C
< prev
next >
Wrap
Text File
|
1992-12-02
|
6KB
|
242 lines
/*
* MacShell Source File
*
* Copyright (c) 1989, 1990, 1991, 1992 Suick Bay Technologies. All rights reserved.
*
*
* RESTRICTIONS ON MacShell program and source code.
*
* Ñ╩MacShell¬ is a product of Suick Bay Technologies and is provided for
* restricted use by the owner of the CDROM "Disk to the future II".
*
* Ñ╩No permission is granted for any commercial use without the written
* consent of the Suick Bay Technologies.
*
* Ñ╩No permission is granted for any redistribution of any kind use without
* the written consent of the Suick Bay Technologies.
*
* Ñ╩Permission is granted to use this for any personal noncommercial use.
*
* Ñ╩You may not distribute source or executable code at all, nor may you
* distribute it with or within a commercial product without the written
* consent of the Suick Bay Technologies. Please send modifications to
* the author for inclusion in updates to the program. Thanks.
*
*
* MacShell¬ IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
* WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
* PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
*
* SUICK BAY TECHNOLOGIES SHALL HAVE NO LIABILITY WITH RESPECT TO THE
* INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY MACSHELL
* OR ANY PART THEREOF.
*
* In no event will Suick Bay Technologies be liable for any lost revenue
* or profits or other special, indirect and consequential damages, even if
* Suick Bay Technologies has been advised of the possibility of such damages.
*
* Suick Bay Technologies can be reached at:
*
* 8768 Cottonwood lane
* Maple Grove, MN 55369
* Voice: (612) 425-7025
* AppleLink: D5233
*
*
* No parts of this software may be reproduced or stored in a
* retrieval system or transmitted in any form, or any means,
* electronic, mechanical, photocopying, recording or otherwise,
* without the prior written permission of Suick Bay Technologies.
*
* Spread the word and not the disk.
*
* SPK 012290 : Initial
*
* Shell command line expansion pattern matching functions
*
* * Match any characters including none
* ? Match any single character
* [╔] Match any enclosed character
* Note that ranges may be specified by [a-Z]
* [╔]+ Match any enclosed character or sequence of chars.
*/
#include <ctype.h>
#include "SystemPub.h"
#include "Prefs.h"
static Boolean caseIns = FALSE;
/*******************************************************************/
void SetCaseIns( Boolean flag )
{
caseIns = flag;
}
Boolean GetCaseIns( void )
{
return caseIns;
}
/*******************************************************************/
int strcmpi( char *s, char *t )
{
for( ; tolower( *s ) == tolower( *t ); s++, t++ )
if( *s == '\0' )
return( 0 );
return( *s - *t );
}
int strcmp( char *s, char *t )
{
if( caseIns || ShellPrefs.caseIns )
return( strcmpi( s, t ) );
for( ; *s == *t; s++, t++ )
if( *s == '\0' )
return( 0 );
return( *s - *t );
}
/*******************************************************************/
Boolean IllegalPat( char *pattern )
{
int c;
while( *pattern )
{
c = *pattern++;
if( c == '[' ) /* look for matching ']' */
{
while( c = *pattern++ )
if( c == ']' )
break;
if( !c ) /* didn't find matching ']' */
return( TRUE );
}
}
return( FALSE );
}
/*******************************************************************/
Boolean StrPatMatch( char *string, char *pattern )
{
char c, n, f, e, cc; /* character */
char *b; /* beginning of bracket compare */
Boolean flag;
if( IllegalPat( pattern ) )
return( FALSE );
if( string == NULL || pattern == NULL )
return( FALSE );
while( *pattern )
if ( *pattern == '?' ) /* match any single character */
{
string++;
pattern++;
}
else
{
c = *pattern++; /* next character */
if( c == '\\' )
{
switch( *pattern )
{
case '*':
case '[':
case ']':
case '+':
case '?':
c = *pattern++;
break;
default:
c = *pattern;
break;
}
}
if( caseIns || ShellPrefs.caseIns )
c = tolower( c );
if ( c == '*' ) /* match any sequence of characters */
{
n = *pattern; /* matches until next char in pat string */
if( caseIns || ShellPrefs.caseIns ) /* force case if needed */
n = tolower( n );
/*
* Needs a fix for *[aaa] and *? and [asd]+ and **
*/
if( caseIns || ShellPrefs.caseIns ) /* force case if needed */
while ( tolower( *string ) != n && *string )
string++;
else
while ( *string != n && *string )
string++;
if( !*string ) /* end of string */
return( n == '\0' );
}
else if ( c == '[' )
{ /* matches any char up to ']' in pattern */
b = pattern;
f = *b; /* for range checking */
while( *b != ']' )
{
if( *b == '\\' )
{
if( *string == *++b ) /* we matched the character */
break;
}
if( *string == *b ) /* we matched the character */
{
*string++;
break;
}
else if( *b == '-' )
{
e = *++b; /* end of range check */
if( *string >= f && *string <= e )
{
string++;
break; /* we matched the range */
}
}
f = *b++;
}
if( *b == ']' ) /* no match, exausted the patern */
return( FALSE );
else
while( *pattern++ != ']' );
}
else
{
cc = *string; /* get character */
if( caseIns || ShellPrefs.caseIns ) /* force case if needed */
cc = tolower( cc );
if ( c == cc ) /* compare */
string++;
else
return( FALSE ); /* no match */
}
}
return( *string == '\0' );
}